package top.infopub.mgr.bpa.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.bpa.OtherContractsInventoryDto;
import top.infopub.dto.bpa.OtherContractsReviewDto;
import top.infopub.mgr.bpa.dao.BpaContractBpaReviewDao;
import top.infopub.mgr.bpa.dao.OtherContractsInventoryDao;
import top.infopub.mgr.bpa.dao.OtherContractsReviewDao;
import top.infopub.mgr.bpa.domain.OtherConInventoryDomain;
import top.infopub.mgr.bpa.service.OtherContractsInventoryService;
import top.infopub.mgr.bpa.vo.OtherConInventoryVo;
import top.infopub.model.bpa.Basic;
import top.infopub.model.bpa.BpaContractListing;
import top.infopub.model.bpa.OtherContractsInventory;
import top.infopub.model.bpa.OtherContractsReview;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 其他合同清单录 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-06-18
 */
@Service
public class OtherContractsInventoryServiceImpl implements OtherContractsInventoryService {
    @Autowired
    OtherContractsInventoryDao otherContractsInventoryDao;
    @Autowired
    OtherContractsReviewDao otherContractsReviewDao;
    @Autowired
    BpaContractBpaReviewDao bpaContractBpaReviewDao;

    @Override
    public Page<OtherConInventoryDomain> selectPageByDomainWhere(Page<OtherConInventoryDomain> page, OtherConInventoryVo otherConInventoryVo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<OtherConInventoryDomain>) otherContractsInventoryDao.selectByDomainWhere(otherConInventoryVo);
    }

    //下拉框数据
    @Override
    public List<Map<String, String>> projectDropDownBox() {
        return otherContractsInventoryDao.projectDropDownBox();
    }

    @Override
    public OtherContractsInventoryDto queryById(String id) {
        OtherContractsInventoryDto dto = new OtherContractsInventoryDto();
        //根据id查询主表数据
        OtherContractsInventory otherContractsInventory = otherContractsInventoryDao.findById(id);
        //根据主表数据的procode查询 项目基本信息
        String proCode = otherContractsInventory.getProCode();
        if (!StringUtils.isEmpty(proCode)) {
            Basic basic = otherContractsInventoryDao.findByProCode(proCode);
            String projectLeader = otherContractsInventory.getProjectLeader();
            if (!StringUtils.isEmpty(projectLeader)){
                basic.setProjectLeader(projectLeader);
            }
            dto.setBasic(basic);
        }
        //主表数据
        if (otherContractsInventory != null) {
            dto.setOtherContractsInventory(otherContractsInventory);
        }
        //合同清单信息
        List<BpaContractListing> list = bpaContractBpaReviewDao.selectListing(id, "9");
        dto.setBpaContractListingList(list);
        return dto;
    }

    @Override
    public int save(OtherContractsInventoryDto dto) {
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        OtherContractsInventory otherContractsInventory = dto.getOtherContractsInventory();
        String id = otherContractsInventory.getId();
        if (StringUtils.isEmpty(id)) {
            //第一次 保存
            otherContractsInventory.setAddBy(vo.getId());
            otherContractsInventory.setAddTime(new Date());
            otherContractsInventory.setProcessStatus("0");
            //保存主表数据
            otherContractsInventoryDao.insertOtherContractsInventory(otherContractsInventory);
        }else {
            //修改
            otherContractsInventory.setUptBy(vo.getId());
            otherContractsInventory.setUptTime(new Date());
            otherContractsInventoryDao.updateOtherContractsInventory(otherContractsInventory);
        }
        //删除合同清单录入数据
        bpaContractBpaReviewDao.deleteListing(id,"9");
        //保存合同清单录入数据
        int seq2 = 0;
        List<BpaContractListing> bpaContractListingList = dto.getBpaContractListingList();
        for (BpaContractListing bpaContractListing : bpaContractListingList) {
            bpaContractListing.setType("9");
            bpaContractListing.setContractId(id);
            bpaContractListing.setSeq(seq2++);
        }
        if (bpaContractListingList!=null){
            bpaContractBpaReviewDao.insertBatchListing(bpaContractListingList);
        }
        return 1;
    }

    @Override
    public OtherContractsInventory findById(String id) {
        return otherContractsInventoryDao.findById(id);
    }
}
